package com.util;

import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BaseDao {

    static{
        // 加载JDBC驱动
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("JDBC Driver not found.");
            e.printStackTrace();
        }
    }

   // 192.168.31.227
//192.168.43.26
    public static Connection getConn() {
        String connectionUrl = "jdbc:sqlserver://192.168.31.227:1433;databaseName=PManage;encrypt=true;trustServerCertificate=true;";
        String username = "sa";
        String password = "xuchenjie";


        // 建立连接
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(connectionUrl, username, password);
            System.out.println("Connection established successfully.");
            // 这里可以执行数据库操作
        } catch (SQLException e) {
            System.out.println("Error establishing connection.");
            e.printStackTrace();
        }
        return connection;
    }

    public static void main(String[] args) {
        Connection conn = getConn();
    }
}


